﻿
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class UI_Animation_ReadyTime : MonoBehaviour
{
    [SerializeField]
    private Image m_loadingGround;
    [SerializeField]
    private Image m_readyNumber;

    private Sprite[] m_numberSprites;
    private Sprite[] m_loadingSprites;
    private void OnEnable()
    {
        if (m_numberSprites == null)
            m_numberSprites = Resources.LoadAll<Sprite>("Images/UI/Title/ReadyNumber");
        if (m_loadingSprites == null)
            m_loadingSprites = Resources.LoadAll<Sprite>("Images/UI/Title/Loading");
    }

    private Coroutine m_AnimationCorutine;
    public void ShowCurTimeAnimation(int pCurTime)
    {
        if (m_AnimationCorutine != null) StopCoroutine(m_AnimationCorutine);
        m_AnimationCorutine = StartCoroutine(m_ResetNumber(pCurTime));
    }
    private IEnumerator m_ResetNumber(int pCurTime)
    {
        var size = m_readyNumber.rectTransform.sizeDelta;
        m_readyNumber.sprite = m_numberSprites[pCurTime];
        m_readyNumber.rectTransform.sizeDelta = size * 0.8f;
        m_readyNumber.rectTransform.DOSizeDelta(size, 0.3f);
        for (var loadIndex = 0; loadIndex < m_loadingSprites.Length; loadIndex++)
        {
            m_loadingGround.sprite = m_loadingSprites[loadIndex];
            yield return new WaitForSeconds(1f / m_loadingSprites.Length);
        }
    }

}
